<?php
    ob_start("ob_gzhandler");
    require_once "../includes/functions.php";
?>
<!DOCTYPE html>
<html>
    <head>
        <link type="text/css" href="/rise/css/flick/jquery-ui-1.8.10.custom.css" rel="stylesheet"/>
        <script type="text/javascript" src="/rise/js/jquery-1.4.4.min.js"></script>
        <script type="text/javascript" src="/rise/js/jquery-ui-1.8.10.custom.min.js"></script>
        <style type="text/css">* { font-family: Tahoma; } img,a { border:0;outline:0; }</style>    
        <script type="text/javascript">
            var checkLoginIntervalId = 0;
            function readCookie(name) {
                var nameequals = name+'=';
                var ca = document.cookie.split(';');
                for (var i=0; i<ca.length;i++) {
                    var c = ca[i];
                    while (c.charAt(0)==' ') c = c.substring(1,c.length);
                    if (c.indexOf(nameequals)==0) return c.substring(nameequals.length,c.length);
                }
                return false;
            }
            function get_recommendations () {
                $('#recs').html('');
                $('#loading').show();
                $.ajax({
                    url: '/rise/tools/recs.php',
                    success: function(data) {
                        $('#recs').html(data);
                        $('#loading').hide();
                    }
                });
            }
            function check_login() {
                if (readCookie('ourisegadgetauth') != '') {
                    get_recommendations();
                    clearInterval(checkLoginIntervalId);
                }
            }
            function process_search (ui,page) {
                var terms = ui.item ? ui.item.value : $('#query').val();
                var sBy = $('input:radio[name=searchBy]:checked').val();
                var urlstr = '/rise/tools/search.php?q='+escape(terms)+'&type='+sBy+'&page='+page+'&source=gadget';
                $('#search-results').hide();
                $('#loading').show();
                $.ajax({
                    url: urlstr,
                    success: function(data) {
                        $('#search-results').show().html(data);
                        $('#query').blur();
                        $('#loading').hide();
                        $('#logos').hide();
                        $('#search-results').focus();
                    }
                });
            }
            $('#search').live('submit', function(e) {
                e.preventDefault();
                process_search('',1);
            });
            $('#login').live('click',function(e) {
                checkLoginIntervalId = setInterval('check_login()',2000);
            });
            $(function() {
                $('#query').autocomplete({
                    source: '/rise/tools/search_suggest.php',minLength: 0,
                    select: function(e,ui) { process_search(ui,1); }
                });
            });
            var _gaq = _gaq || []; _gaq.push(['_setAccount', YOUR_ANALYTICS_ID]); _gaq.push(['_trackPageview','/gadget']); (function() {
                var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
                ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
                var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
            })();
        </script>
    </head>
    <body>   
        <style>body,td{font-family:arial,sans-serif;font-size:smaller;}</style>
        <img style="float:right;" src="/includes/headers-footers/oulogo-40.jpg"/>
        <form style="border-bottom:1px solid #DEDEDE;padding-bottom:3px;" id="search" method="post" action="">
            <b>Search for:</b> <input type="text" id="query" name="query"/><br/>
            <b>in</b>&nbsp;<span style="font-size:12px;"><input type="radio" name="searchBy" id="Keyword" value="Keyword" checked="checked"/><label for="Keyword">Keyword</label>
               <input type="radio" name="searchBy" id="Title" value="Title"/><label for="Title">Title</label>
               <input type="radio" name="searchBy" id="Author" value="Author"/><label for="Author">Author</label></span>&nbsp;<input type="submit" style="" value="Go"/>
        </form>
        <div id="loading" style="text-align:center;display:none;">
            <img src="/rise/images/gadget_loading.gif" style="width:16px;margin:0 auto;" alt=""/><br/>Loading Search Results...
        </div>
        <div id="search-results" style="display:none;"></div>    
<?php
    echo "<div style=\"margin-top:12px;padding-top:5px;border-top:1px solid #DEDEDE;\" id=\"recs\">\n";
    function display_login() {
        echo "<br/>Please <a target=\"_blank\" id=\"login\" href=\"http://library.open.ac.uk/rise/?page=gadget_auth\">Log In</a> to receive personalized recommendations<br/>";#
    }
    $cookie_auth = check_cookie_auth();
    if ($cookie_auth!==false) {
        $combined_recommendations = get_combined_recommendations($cookie_auth,'array','',3);
        if (count($combined_recommendations)>0) {
            echo "  <b>Recommendations:</b>\n";
            echo "  <ol style=\"margin-top:3px;padding-left:0;\">";
            foreach ($combined_recommendations as $val) {
                echo "      <li><a target=\"_blank\" href=\"{$val['link']}\">".ssreplace($val['name'],40)."</a></li>";
            }
            echo "  </ol>";
        }
    }else { display_login(); }
    
    echo "</div>\n";
?>
        <div id="logos" style="margin-top:30px;text-align:center;margin-left:-10px;">
            <img src="/rise/images/separator.jpg" style="clear:both;"/>
            <a target="_blank" href="http://www.open.ac.uk/blogs/RISE" title="Recommendations Improve the Search Experience"><img src="/rise/images/rise_small_logo.jpg" style="float:left;margin-top:2px;margin-left:35px;"/></a>
            <a target="_blank" href="http://www.jisc.org.uk" title="JISC"><img src="/rise/images/jisc_small_logo.jpg" style="float:right;margin-right:35px;"/></a>
            <img src="/rise/images/separator.jpg" style="clear:both;"/>
        </div>
        <div style="position:absolute;bottom:0; width:100%;text-align:center;"><a target="_blank" href="http://library.open.ac.uk/rise/?page=FAQ" title="Frequently Asked Questions">FAQ</a> | <a href="http://library.open.ac.uk/rise/?page=privacy" target="_blank">Privacy Policy</a></div>
    </body>
</html>
<?php ob_end_flush(); ?>